package checkerboard;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Checkerboard extends javax.swing.JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5793357804828609325L;
	private final int max = 10;
	private int current = 0;
	private JPanel panel;
	private JButton[] buttons;
	private JButton load;

	public Checkerboard() {
		this.setResizable(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		panel = new JPanel(new GridLayout());
		buttons = new JButton[max];
		for (int i = 0; i < max; i++) {
			buttons[i] = new JButton(Integer.toString(i));
			panel.add(buttons[i]);
			buttons[i].addActionListener(this);
		}
		load = new JButton("Load");
		load.addActionListener(this);
		load.setActionCommand("load");
		panel.add(load);
		load.setEnabled(false);
		this.add(panel);
		this.pack();
		this.setVisible(true);
	}

	public static void main(String[] args) {
		@SuppressWarnings("unused")
		Checkerboard ch = new Checkerboard();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("load")) {
			JOptionPane.showMessageDialog(this, 
					"loader.load("+Integer.toString(current)+")");
		} else {
			int i = Integer.parseInt(e.getActionCommand());
			if (current != i) {
				buttons[current].setSelected(false);
				buttons[i].setSelected(true);
				current = i;
			} else
				{
				buttons[i].setSelected(! buttons[i].isSelected());
				}
			load.setEnabled(buttons[i].isSelected());

		}
	}
}
